//
//  NewDataBase.m
//  New
//
//  Created by dlios on 15-7-24.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "NewDataBase.h"

@implementation NewDataBase

+(instancetype)sharDataBase{

    static NewDataBase *newDataBase;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        newDataBase = [[NewDataBase alloc]init];
    });
    return newDataBase;
    
}
static sqlite3 *db = nil;

//打开数据库
-(void)OpenDB{
    
    if (db != nil) {
        return;
    }
    NSArray *array  =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"dataBase.sqlite"];
//    NSLog(@"***%@",sqlitePath);
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result ==SQLITE_OK) {
        //        NSLog(@"打开数据库成功");
    }else
    {
        
        NSLog(@"打开数据库失败");
    }
    
}
//创建列表
-(void)creatTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS News(number INTEGER PRIMARY KEY AUTOINCREMENT,ID TEXT,title TEXT,url TEXT,imageUrl TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//                NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
        
    }
}

//创建头部视图列表
-(void)creatTopTable{

    NSString *sql = @"CREATE TABLE IF NOT EXISTS NewsDetail(number INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,imageUrl TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
        
    }


}


-(void)insertNewModel:(NewModel *)newMod{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO News(ID,title,url,imageUrl)VALUES('%@','%@','%@','%@')",newMod.ID,newMod.title,newMod.url,newMod.imageUrl];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//                NSLog(@"添加成功");
    }else
    {
        
        NSLog(@"添加失败");
    }
    
    
}
//添加头部视图
-(void)insertNewDetailModel:(HeadleModel *)headelMod{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO NewsDetail(title,imageUrl)VALUES('%@','%@')",headelMod.title,headelMod.web];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//        NSLog(@"添加成功");
    }else
    {
        
        NSLog(@"添加失败");
    }
    
    
}

-(NSMutableArray *)selectAllNews{
    
    NSString *sql = @"SELECT * FROM News";
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result ==SQLITE_OK) {
        //        NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            const unsigned char *ID = sqlite3_column_text(stmt, 1);
            const unsigned char *title = sqlite3_column_text(stmt, 2);
            const unsigned char *url = sqlite3_column_text(stmt, 3);
            const unsigned char *imageUrl = sqlite3_column_text(stmt, 4);
            
            
            NewModel *newMod = [[NewModel alloc]init];
            newMod.ID = [NSString stringWithUTF8String:(const char *)ID];
            newMod.title = [NSString stringWithUTF8String:(const char *)title];
            newMod.url = [NSString stringWithUTF8String:(const char *)url];
            newMod.imageUrl = [NSString stringWithUTF8String:(const char *)imageUrl];
          
            [array addObject:newMod];
        }
        
        
        return array;
    }else{
        NSLog(@"准备失败");
        return array;
        
    }
    
    
}

//查找头部列表
-(NSMutableArray *)selectAllNewsDetail{
    
    NSString *sql = @"SELECT * FROM NewsDetail";
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result ==SQLITE_OK) {
        //        NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
           
            const unsigned char *title = sqlite3_column_text(stmt, 1);
           
            const unsigned char *imageUrl = sqlite3_column_text(stmt, 2);
            
            
            HeadleModel *heldMod = [[HeadleModel alloc]init];
           
            heldMod.title = [NSString stringWithUTF8String:(const char *)title];
           
            heldMod.web = [NSString stringWithUTF8String:(const char *)imageUrl];
            
            [array addObject:heldMod];
        }
        
        
        return array;
    }else{
        NSLog(@"准备失败");
        return array;
        
    }
    
    
}


-(void)dropTable
{
    NSString *sql = @"DROP TABLE News";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//                NSLog(@"删除成功");
    }else
    {
        
        NSLog(@"删除失败");
        
    }
 
    
}
-(void)dropTopTable
{
    NSString *sql = @"DROP TABLE NewsDetail";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//        NSLog(@"删除成功");
    }else
    {
        
        NSLog(@"删除失败");
        
    }
    
    
}






@end
